using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XBLib.Common
{
    public class ErrorCodeHelper
    {
        private static readonly ErrorCodeHelper _instance = new ErrorCodeHelper();
        public static string ERRORCODEFILEPATH = "errorCode.txt";
        public static string SEPARATOR = "-";

        private ErrorCodeHelper()
        { }

        public static ErrorCodeHelper Instance()
        {
            return _instance;
        }

        public string GetErrorString(int errorCode, params string[] args)
        {
            string template = getErrorTemplate(errorCode);
            if (template == string.Empty)
                return string.Empty;
            return string.Format(template, args);
        }

        private string getErrorTemplate(int errorCode)
        {
            string line = string.Empty;
            string template = string.Empty;
            int code = 0;
            int index = -1;
            StreamReader sr = new StreamReader(ERRORCODEFILEPATH);
            while ((line = sr.ReadLine()) != string.Empty)
            {
                index = line.IndexOf(SEPARATOR);
                if (index <= 0)
                {
                    template = string.Empty;
                    break;
                }
                code = int.Parse(line.Substring(0, index));
                if (code != errorCode)
                    continue;
                template = line.Substring(index + 1, line.Length - index - 1);
                break;
            }
            return template;
        }
    }
}
